package net.lab1024.sa.admin.module.business.flow.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.business.flow.domain.form.FlwDeModelAddForm;
import net.lab1024.sa.admin.module.business.flow.domain.form.FlwDeModelQueryForm;
import net.lab1024.sa.admin.module.business.flow.domain.form.FlwDeModelUpdateForm;
import net.lab1024.sa.admin.module.business.flow.domain.form.FlwDeployQueryForm;
import net.lab1024.sa.admin.module.business.flow.domain.vo.FlwDeModelVO;
import net.lab1024.sa.admin.module.business.flow.domain.vo.FlwDeployProDefVO;
import net.lab1024.sa.admin.module.business.flow.domain.vo.FlwDeployVO;
import net.lab1024.sa.admin.module.business.flow.service.FlwDeModelService;
import net.lab1024.sa.admin.module.business.flow.service.FlwDeployService;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.ValidateList;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 流程设计 Controller
 *
 * @Author ADog
 * @Date 2025-10-06 20:16:41
 * @Copyright 1.0
 */

@Slf4j
@RestController
@RequestMapping("/flw/deploy")
@Tag(name = "流程部署信息")
public class FlwDeployController {

    @Resource
    private FlwDeployService flwDeployService;

    @Operation(summary = "分页查询 @author ADog")
    @PostMapping("/list")
    @SaCheckPermission("flwDeploy:query")
    public ResponseDTO<PageResult<FlwDeployVO>> queryPage(@RequestBody @Valid FlwDeployQueryForm queryForm) {
        return ResponseDTO.ok(flwDeployService.queryPage(queryForm));
    }

    @Operation(summary = "分页查询 @author ADog")
    @PostMapping("/listProcDef")
    @SaCheckPermission("flwDeploy:query")
    public ResponseDTO<PageResult<FlwDeployProDefVO>> listProcDef(@RequestBody @Valid FlwDeployQueryForm queryForm) {
        return ResponseDTO.ok(flwDeployService.listProcDef(queryForm));
    }

    /**
     * 根据id查询流程图
     */
    @Operation(summary = "根据id查询流程图 @author ADog")
    @GetMapping("/getProcessDiagram/{id}")
    @SaCheckPermission("flwDeploy:query")
    public ResponseDTO<byte[]> getProcessDiagram(@PathVariable String id) {
        log.info("FlwDeployController.getProcessDiagram：{}", id);
        byte[] bytes = flwDeployService.getProcessDiagram(id);
        return ResponseDTO.ok(bytes);
    }

    /**
     * 根据id查询流程定义的xml文件
     */
    @Operation(summary = "根据id查询流程图 @author ADog")
    @GetMapping("/queryFlowXml/{id}")
    @SaCheckPermission("flwDeploy:query")
    public ResponseDTO<String> queryFlowXml(@PathVariable String id) {
        log.info("FlwDeployController.id：{}", id);
        String xml = flwDeployService.queryFlowXml(id);
        return ResponseDTO.ok(xml);
    }

    /**
     * 根据id查询流程定义信息用于发起流程
     */
    @Operation(summary = "根据id查询流程图 @author ADog")
    @GetMapping("/getFlowDef/{id}")
    @SaCheckPermission("flwDeploy:query")
    public ResponseDTO<List<Map<String,Object>>> getFlowDef(@PathVariable String id) {
//        通过bpmnmodel对象处理
        List<Map<String,Object>> flowMessage = flwDeployService.getFlowDef(id);
        return ResponseDTO.ok(flowMessage);
    }
}
